// 格式化时间函数
export function formatDate(getDate: number, format: string) {
  const date = new Date(getDate)
  console.log(date)
  const Y = `${date.getFullYear()}`
  //.slice(2)
  const M =
    `${date.getMonth() + 1}`.length === 1 ? `0${date.getMonth() + 1}` : `${date.getMonth() + 1}`
  const D = `${date.getDate()}`.length === 1 ? `0${date.getDate()}` : `${date.getDate()}`
  const H = `${date.getHours()}`.length === 1 ? `0${date.getHours()}` : `${date.getHours()}`
  const m = `${date.getMinutes()}`.length === 1 ? `0${date.getMinutes()}` : `${date.getMinutes()}`
  const s = `${date.getSeconds()}`.length === 1 ? `0${date.getSeconds()}` : `${date.getSeconds()}`

  return format
    .replace(/(Y+)/, Y)
    .replace(/(M+)/, M)
    .replace(/(D+)/, D)
    .replace(/(H+)/, H)
    .replace(/(m+)/, m)
    .replace(/(s+)/, s)
}

export const dateObj = ref()

/**
 * @desc 计算两个时间戳的时间差函数
 * @author 玫瑰少年 <2412270464@qq.com>
 * @Date: 2024-03-01
 * @func timeDiff
 * @param {number}   nowDate 当前时间戳
 * @param {number}   dateValue 后端传值的时间戳
 * @return {string}  string
 */
export function timeDiff(dateValue: number): (string | number)[] {
  const newDay = dateValue / (24 * 60 * 60 * 1000)
  const day = Math.floor(newDay)

  const newH = ((newDay - day) * 24) % 24
  const H = Math.floor(newH)

  const newM = ((newH - H) * 60) % 60
  const m = Math.floor(newM)

  const newS = ((newM - m) * 60) % 60
  const S = Math.floor(newS)

  const dateData = { day: day, H: H, m: m, S: S }
  dateObj.value = dateData
  // return `${dateData.day}天 ${dateData.H}:${dateData.m}:${dateData.S}秒`
  if (dateData.day > 0) return [dateData.day, '天']
  else if (dateData.H > 0 && dateData.day == 0) return [dateData.H, `时`]
  else if (dateData.m > 0 && dateData.day == 0) return [dateData.m, `分`]
  else if (dateData.S > 0 && dateData.day == 0) return [dateData.S, `秒`]
  else return [-1, '任务已超时']
}

export function timeNumber(time: string | number | Date) {
  const timestamp = Date.parse(new Date(time).toString())
  // console.log(time + '的时间戳为：' + timestamp)
  return timestamp
}
